unit uTranslate;

interface

uses
  SysUtils, Classes, Contnrs, uWebTranslate, uConst;

type
  TDoTranslate = class(TObject)
  private
    FTsClasslist: TObjectList;
    FOnTranslateBegin: TGetPCharProc;
    FOnTranslateComplete: TGetPCharProc;
    function GetTSInstance(AWebTranslateClass: TWebTranslateClass): TWebTranslate;
    function TSCompleted: Boolean;
  public
    procedure DoTranslate(ASrc: WideString; ALangSrc, ALangTar: Integer;
      AtsType: TtsType; AConfiglist: TStrings); overload;
    procedure DoTranslate(ASrc: WideString; ALangSrc, ALangTar: Integer;
      AtsType: TtsType; EngineName: pWideChar); overload;
    procedure Cancel;

    constructor Create;
    destructor Destroy; override;

    property Completed: Boolean read TSCompleted;

    property OnTranslateBegin: TGetPCharProc read FOnTranslateBegin write FOnTranslateBegin;
    property OnTranslateComplete: TGetPCharProc read FOnTranslateComplete write FOnTranslateComplete;
  end;

var
  DoTranslate: TDoTranslate;

  procedure Translate(ASrc: pWideChar; ALangSrc, ALangTar: Integer;
    TranslateBegin, TranslateComplete: TGetPCharProc); stdcall;
  procedure Cancel; stdcall;
  function Completed: Boolean; stdcall;
  procedure UpdateConfig; stdcall;

  function getEngineList(): pWideChar; stdcall;
  procedure TranslateEx(ASrc: pWideChar; ALangSrc, ALangTar: Integer;
    EngineName: pWideChar; TranslateBegin, TranslateComplete: TGetPCharProc); stdcall;

implementation

function getEngineList(): pWideChar;
var
  idx: Integer;
  n: WideString;
begin
  for idx := 0 to Length(TWebTranslate.EngineArr) - 1 do
  begin
    n := n + TWebTranslate.EngineArr[idx].Flag + ',';
  end;
  Result := pWideChar(n);
end;

procedure UpdateConfig;
begin
  SysCfg.LoadCfg;
end;

procedure Translate(ASrc: pWideChar; ALangSrc, ALangTar: Integer;
  TranslateBegin, TranslateComplete: TGetPCharProc);
begin
  DoTranslate.OnTranslateBegin := TranslateBegin;
  DoTranslate.OnTranslateComplete := TranslateComplete;

  DoTranslate.DoTranslate(ASrc, ALangSrc, ALangTar, tsDict, SysCfg.TSlst);
end;

procedure TranslateEx(ASrc: pWideChar; ALangSrc, ALangTar: Integer;
  EngineName: pWideChar; TranslateBegin, TranslateComplete: TGetPCharProc);
begin
  DoTranslate.OnTranslateBegin := TranslateBegin;
  DoTranslate.OnTranslateComplete := TranslateComplete;

  DoTranslate.DoTranslate(ASrc, ALangSrc, ALangTar, tsText, EngineName);
end;

procedure Cancel;
begin
  DoTranslate.Cancel;
end;

function Completed: Boolean;
begin
  Result := DoTranslate.Completed;
end;

{ TDoTranslate }

procedure TDoTranslate.Cancel;
var
  idx: Integer;
begin
  for idx := FTsClasslist.Count - 1 downto 0 do
  begin
    if TWebTranslate(FTsClasslist.Items[idx]).Working then
      TWebTranslate(FTsClasslist.Items[idx]).Cancel;
  end;
end;

constructor TDoTranslate.Create;
begin
  FTsClasslist := TObjectList.Create;
end;

destructor TDoTranslate.Destroy;
begin
  FreeAndNil(FTsClasslist);
  inherited;
end;

procedure TDoTranslate.DoTranslate(ASrc: WideString; ALangSrc,
  ALangTar: Integer; AtsType: TtsType; EngineName: pWideChar);
var
  mtsEngine: TWebTranslate;
  mtsEngineInfo: TtsEngineInfo;
begin
  for mtsEngineInfo in TWebTranslate.EngineArr do
  begin
    if SameText(mtsEngineInfo.Flag, EngineName) then
    begin
      mtsEngine := GetTSInstance(mtsEngineInfo.tsEngine);
      mtsEngine.Src := ASrc;
      mtsEngine.LangSrc := ALangSrc;
      mtsEngine.LangTar := ALangTar;
      mtsEngine.Translate();
      Break;
    end;
  end;
end;

function TDoTranslate.GetTSInstance(AWebTranslateClass: TWebTranslateClass): TWebTranslate;
var
  idx: Integer;
begin
  idx := FTsClasslist.FindInstanceOf(AWebTranslateClass);
  if idx>=0 then
  begin
    Result := TWebTranslate(FTsClasslist.Items[idx]);
  end
  else begin
    Result := AWebTranslateClass.Create;
    Result.OnTranslateBegin := FOnTranslateBegin;
    Result.OnTranslateComplete := FOnTranslateComplete;
    FTsClasslist.Add(Result);
  end;
end;

procedure TDoTranslate.DoTranslate(ASrc: WideString; ALangSrc, ALangTar: Integer;
  AtsType: TtsType; AConfiglist: TStrings);
var
  mtsEngine: TWebTranslate;
  mtsEngineInfo: TtsEngineInfo;
begin
  for mtsEngineInfo in TWebTranslate.EngineArr do
  begin
    if (AConfiglist.Values[mtsEngineInfo.Flag]='1')
      and (mtsEngineInfo.tsType=AtsType) then
    begin
      mtsEngine := GetTSInstance(mtsEngineInfo.tsEngine);
      mtsEngine.Src := ASrc;
      mtsEngine.LangSrc := ALangSrc;
      mtsEngine.LangTar := ALangTar;
      mtsEngine.Translate();
    end;
  end;
end;

function TDoTranslate.TSCompleted: Boolean;
var
  idx: Integer;
begin
  Result := True;
  for idx := FTsClasslist.Count - 1 downto 0 do
  begin
    if TWebTranslate(FTsClasslist.Items[idx]).Working then
    begin
      Result := False;
      Break;
    end;
  end;
end;

initialization
  DoTranslate := TDoTranslate.Create;

finalization
  FreeAndNil(DoTranslate);

end.
